using System.Text.Json.Serialization;

namespace AgentFrameworkDemo.Models;

/// <summary>
/// Microsoft Agent Framework 核心模型
/// 基于官方文档架构设计
/// </summary>

/// <summary>
/// Agent生命周期状态
/// </summary>
public enum AgentLifecycleState
{
    Created,     // 已创建
    Creating,    // 创建中
    Ready,       // 就绪
    Running,     // 运行中
    Paused,      // 暂停
    Stopping,    // 停止中
    Stopped,     // 已停止
    Error        // 错误状态
}

/// <summary>
/// Agent定义
/// </summary>
public class AgentDefinition
{
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string Name { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public AgentCapabilities Capabilities { get; set; } = new();
    public AgentConfiguration Configuration { get; set; } = new();
    public List<string> Tags { get; set; } = new();
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public Dictionary<string, object> Metadata { get; set; } = new();

    /// <summary>
    /// 自定义系统提示模板，如果为空则使用配置文件中的默认模板
    /// </summary>
    public string? CustomSystemPrompt { get; set; }

    /// <summary>
    /// 系统提示模板类型，用于从配置文件中选择预设模板
    /// </summary>
    public string SystemPromptTemplate { get; set; } = "DefaultTemplate";
}

/// <summary>
/// Agent能力定义
/// </summary>
public class AgentCapabilities
{
    public bool EnableMemory { get; set; } = true;
    public bool EnableTools { get; set; } = true;
    public bool EnableWorkflows { get; set; } = true;
    public bool EnableMultiModal { get; set; } = false;
    public List<string> SupportedToolTypes { get; set; } = new();
    public List<string> SupportedWorkflowTypes { get; set; } = new();
}

/// <summary>
/// Agent配置
/// </summary>
public class AgentConfiguration
{
    public AIModelConfig Model { get; set; } = new();
    public MemoryConfig Memory { get; set; } = new();
    public SecurityConfig Security { get; set; } = new();
    public PerformanceConfig Performance { get; set; } = new();
}

/// <summary>
/// AI模型配置
/// </summary>
public class AIModelConfig
{
    public string Provider { get; set; } = "OpenAI";
    public string ModelId { get; set; } = "gpt-4";
    public string? Endpoint { get; set; }
    public string ApiKey { get; set; } = string.Empty;
    public int MaxTokens { get; set; } = 4000;
    public double Temperature { get; set; } = 0.7;
    public Dictionary<string, object> Parameters { get; set; } = new();
}

/// <summary>
/// 记忆配置
/// </summary>
public class MemoryConfig
{
    public bool EnableShortTermMemory { get; set; } = true;
    public bool EnableLongTermMemory { get; set; } = true;
    public bool EnableSemanticMemory { get; set; } = true;
    public int MaxShortTermMessages { get; set; } = 20;
    public int MaxLongTermMemories { get; set; } = 1000;
    public double MemoryRelevanceThreshold { get; set; } = 0.7;
    public string? VectorStoreConnection { get; set; }
}

/// <summary>
/// 安全配置
/// </summary>
public class SecurityConfig
{
    public List<string> AllowedDomains { get; set; } = new();
    public bool AllowFileAccess { get; set; } = false;
    public bool AllowNetworkAccess { get; set; } = true;
    public bool AllowCodeExecution { get; set; } = false;
    public int MaxExecutionTime { get; set; } = 300; // 秒
    public List<string> BlockedOperations { get; set; } = new();
    public ContentFilterConfig ContentFilter { get; set; } = new();
}

/// <summary>
/// 内容过滤配置
/// </summary>
public class ContentFilterConfig
{
    public bool Enabled { get; set; } = true;
    public List<string> BlockedContent { get; set; } = new();
    public bool FilterPersonalInfo { get; set; } = true;
    public bool FilterSensitiveData { get; set; } = true;
}

/// <summary>
/// 性能配置
/// </summary>
public class PerformanceConfig
{
    public int MaxConcurrentTasks { get; set; } = 5;
    public int CacheSize { get; set; } = 100;
    public int CacheExpirationMinutes { get; set; } = 60;
    public bool EnableCompression { get; set; } = true;
    public bool EnableStreaming { get; set; } = false;
}

/// <summary>
/// Agent实例
/// </summary>
public class Agent
{
    public string Id { get; set; } = string.Empty;
    public AgentDefinition Definition { get; set; } = new();
    public AgentLifecycleState State { get; set; } = AgentLifecycleState.Created;
    public Dictionary<string, object> RuntimeContext { get; set; } = new();
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime LastActivityAt { get; set; } = DateTime.UtcNow;
    public List<AgentExecution> ExecutionHistory { get; set; } = new();
    public AgentStatistics Statistics { get; set; } = new();
}

/// <summary>
/// Agent执行记录
/// </summary>
public class AgentExecution
{
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string AgentId { get; set; } = string.Empty;
    public DateTime StartedAt { get; set; } = DateTime.UtcNow;
    public DateTime? CompletedAt { get; set; }
    public string Input { get; set; } = string.Empty;
    public string Output { get; set; } = string.Empty;
    public bool Success { get; set; }
    public string? ErrorMessage { get; set; }
    public TimeSpan ExecutionTime { get; set; }
    public ExecutionMetadata Metadata { get; set; } = new();
    public List<ToolExecution> ToolExecutions { get; set; } = new();
}

/// <summary>
/// 执行元数据
/// </summary>
public class ExecutionMetadata
{
    public string? SessionId { get; set; }
    public string? UserId { get; set; }
    public string? TenantId { get; set; }
    public Dictionary<string, object> Parameters { get; set; } = new();
    public Dictionary<string, object> Context { get; set; } = new();
    public List<string> Tags { get; set; } = new();
}

/// <summary>
/// 工具执行记录
/// </summary>
public class ToolExecution
{
    public string ToolId { get; set; } = string.Empty;
    public string ToolName { get; set; } = string.Empty;
    public DateTime StartedAt { get; set; }
    public DateTime? CompletedAt { get; set; }
    public object? Input { get; set; }
    public object? Output { get; set; }
    public bool Success { get; set; }
    public string? ErrorMessage { get; set; }
    public TimeSpan ExecutionTime { get; set; }
}

/// <summary>
/// Agent统计信息
/// </summary>
public class AgentStatistics
{
    public long TotalExecutions { get; set; }
    public long SuccessfulExecutions { get; set; }
    public long FailedExecutions { get; set; }
    public TimeSpan TotalExecutionTime { get; set; }
    public TimeSpan AverageExecutionTime { get; set; }
    public DateTime LastExecutionAt { get; set; }
    public Dictionary<string, long> ToolUsageCounts { get; set; } = new();
    public Dictionary<string, double> PerformanceMetrics { get; set; } = new();
}

/// <summary>
/// Agent请求
/// </summary>
public class AgentRequest
{
    public string AgentId { get; set; } = string.Empty;
    public string Input { get; set; } = string.Empty;
    public Dictionary<string, object> Parameters { get; set; } = new();
    public string? SessionId { get; set; }
    public Dictionary<string, object> Context { get; set; } = new();
    public ExecutionOptions? Options { get; set; }
}

/// <summary>
/// 执行选项
/// </summary>
public class ExecutionOptions
{
    public int? TimeoutSeconds { get; set; }
    public bool EnableStreaming { get; set; } = false;
    public bool IncludeToolCalls { get; set; } = true;
    public double? Temperature { get; set; }
    public int? MaxTokens { get; set; }
    public List<string> EnabledTools { get; set; } = new();
    public List<string> DisabledTools { get; set; } = new();
}

/// <summary>
/// Agent响应
/// </summary>
public class AgentResponse
{
    public bool Success { get; set; }
    public string Content { get; set; } = string.Empty;
    public Dictionary<string, object> Data { get; set; } = new();
    public string? ErrorMessage { get; set; }
    public TimeSpan ExecutionTime { get; set; }
    public string AgentId { get; set; } = string.Empty;
    public string ExecutionId { get; set; } = string.Empty;
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;
    public ResponseMetadata Metadata { get; set; } = new();
    public List<ToolResult> ToolResults { get; set; } = new();
}

/// <summary>
/// 响应元数据
/// </summary>
public class ResponseMetadata
{
    public string ModelUsed { get; set; } = string.Empty;
    public int TokensUsed { get; set; }
    public int PromptTokens { get; set; }
    public int CompletionTokens { get; set; }
    public double Cost { get; set; }
    public Dictionary<string, object> AdditionalData { get; set; } = new();
}

/// <summary>
/// 工具结果
/// </summary>
public class ToolResult
{
    public string ToolId { get; set; } = string.Empty;
    public string ToolName { get; set; } = string.Empty;
    public bool Success { get; set; }
    public object? Result { get; set; }
    public string? ErrorMessage { get; set; }
    public TimeSpan ExecutionTime { get; set; }
    public Dictionary<string, object> Metadata { get; set; } = new();
}

